package com.atstudy.headline.web.admin.controller.advertisement.adCreatives;

import com.atstudy.headline.entity.AdCreatives;
import com.atstudy.headline.enums.BaseStatus;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.admin.service.AdCreativesService;
import com.atstudy.headline.web.admin.vo.ad.AdCreativeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/adCreatives")
public class adCreativesController{
    @Autowired
    private AdCreativesService adCreativeService;

    // 获取创意列表
    @RequestMapping("/page")
    public Result<IPage<AdCreatives>> pageDictInfo(@RequestParam Integer current,
                                                 @RequestParam Integer size,
                                                 AdCreativeVo adCreativeVO){
        Page<AdCreatives> page = new Page<>(current,size);
        LambdaQueryWrapper<AdCreatives> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like( adCreativeVO.getTitle()!=null, AdCreatives::getTitle,adCreativeVO.getTitle());
        queryWrapper.eq( adCreativeVO.getStatus()!=null,AdCreatives::getStatus,adCreativeVO.getStatus());
        IPage<AdCreatives> creativesIPage = adCreativeService.pageInfoByAdCreatives(page,adCreativeVO);
        return Result.ok(creativesIPage);
    }

    /**
     * 增加/修改
     * @param adCreatives
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody AdCreatives adCreatives){
        adCreativeService.saveOrUpdate(adCreatives);
        return Result.ok();
    }

    /**
     * 删除
     * @param creativeId
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer creativeId){
        adCreativeService.removeById(creativeId);
        return Result.ok();
    }

    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer creativeId  ,@RequestParam BaseStatus status) {
        LambdaUpdateWrapper<AdCreatives> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AdCreatives::getCreativeId,creativeId);
        updateWrapper.set(AdCreatives::getStatus,status);
        adCreativeService.update( updateWrapper );
        return Result.ok();
    }
}
